package com.ruoyi.zhmk.service.impl;

import java.util.List;

import com.ruoyi.carApi.click.CarApiClick;
import com.ruoyi.carApi.click.impl.CarApiDefaultClick;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.zhmk.utils.CarApiUtil;
import com.sun.jna.Pointer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhmk.mapper.ZhmkCarDeviceMapper;
import com.ruoyi.zhmk.domain.ZhmkCarDevice;
import com.ruoyi.zhmk.service.IZhmkCarDeviceService;

/**
 * 车牌设备Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-24
 */
@Service
public class ZhmkCarDeviceServiceImpl implements IZhmkCarDeviceService 
{
    @Autowired
    private ZhmkCarDeviceMapper zhmkCarDeviceMapper;

    /**
     * 查询车牌设备
     * 
     * @param id 车牌设备ID
     * @return 车牌设备
     */
    @Override
    public ZhmkCarDevice selectZhmkCarDeviceById(Long id)
    {
        return zhmkCarDeviceMapper.selectZhmkCarDeviceById(id);
    }

    /**
     * 查询车牌设备列表
     * 
     * @param zhmkCarDevice 车牌设备
     * @return 车牌设备
     */
    @Override
    public List<ZhmkCarDevice> selectZhmkCarDeviceList(ZhmkCarDevice zhmkCarDevice)
    {
        List<ZhmkCarDevice> carDevices = zhmkCarDeviceMapper.selectZhmkCarDeviceList(zhmkCarDevice);
        for (ZhmkCarDevice carDevice : carDevices) {
            String ip = carDevice.getIp();
            Pointer pointer = CarApiDefaultClick.connectMap.get(ip);
            if(pointer != null){
                //设备连接成功
                carDevice.setStatus(1);
            }
        }
        return carDevices;
    }

    /**
     * 新增车牌设备
     * 
     * @param zhmkCarDevice 车牌设备
     * @return 结果
     */
    @Override
    public int insertZhmkCarDevice(ZhmkCarDevice zhmkCarDevice)
    {
        //插入设备，并进行连接
        //调用快速连接
        CarApiClick carApiClick = CarApiUtil.getCarApiClick();
        String ip = zhmkCarDevice.getIp();
        String[] ips = {ip};
        carApiClick.quickConnect(ips);
        zhmkCarDevice.setCreateTime(DateUtils.getNowDate());
        return zhmkCarDeviceMapper.insertZhmkCarDevice(zhmkCarDevice);
    }

    /**
     * 修改车牌设备
     * 
     * @param zhmkCarDevice 车牌设备
     * @return 结果
     */
    @Override
    public int updateZhmkCarDevice(ZhmkCarDevice zhmkCarDevice)
    {

        CarApiClick carApiClick = CarApiUtil.getCarApiClick();
        String ip = zhmkCarDevice.getIp();
        String[] ips = {ip};
        carApiClick.quickConnect(ips);
        zhmkCarDevice.setUpdateTime(DateUtils.getNowDate());
        return zhmkCarDeviceMapper.updateZhmkCarDevice(zhmkCarDevice);
    }

    /**
     * 批量删除车牌设备
     * 
     * @param ids 需要删除的车牌设备ID
     * @return 结果
     */
    @Override
    public int deleteZhmkCarDeviceByIds(Long[] ids)
    {
        //获取需要删除的设备列表
        List<ZhmkCarDevice> carDevices = zhmkCarDeviceMapper.selectZhmkCarDeviceByIds(ids);
        CarApiClick carApiClick = CarApiUtil.getCarApiClick();
        for (ZhmkCarDevice carDevice : carDevices) {
            String ip = carDevice.getIp();
            carApiClick.closeConnect(ip);
        }

        return zhmkCarDeviceMapper.deleteZhmkCarDeviceByIds(ids);
    }

    /**
     * 删除车牌设备信息
     * 
     * @param id 车牌设备ID
     * @return 结果
     */
    @Override
    public int deleteZhmkCarDeviceById(Long id)
    {
        ZhmkCarDevice zhmkCarDevice = zhmkCarDeviceMapper.selectZhmkCarDeviceById(id);
        String ip = zhmkCarDevice.getIp();
        CarApiClick carApiClick = CarApiUtil.getCarApiClick();
        carApiClick.closeConnect(ip);
        return zhmkCarDeviceMapper.deleteZhmkCarDeviceById(id);
    }

    /**
     * 开门
     * @param id
     * @return
     */
    @Override
    public AjaxResult openDoor(Long id) {
        //根据id查询出设备信息
        ZhmkCarDevice zhmkCarDevice = zhmkCarDeviceMapper.selectZhmkCarDeviceById(id);
        if(zhmkCarDevice == null){
            throw new CustomException("查询不到设备", HttpStatus.ERROR);
        }
        CarApiClick carApiClick = CarApiUtil.getCarApiClick();
        int i = carApiClick.openGate(zhmkCarDevice.getIp());
        if(i == 1){
            //成功
            return AjaxResult.success();
        }else{
            //失败
            return AjaxResult.error();
        }
    }
}
